﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SBLib.Common.FtpKS
{
    public class FtpFuncKS
    {
        public static System.Net.FtpWebResponse UploadFile(string prmLocalFilePath, string prmFtpFilePath, string prmUser, string prmPassword, bool prmKeepAliveConnection, bool prmUsePassive)
        {
            System.Net.FtpWebRequest _ftpRequest;
            try
            {
                //Get File info
                System.IO.FileInfo _fileInfo = new System.IO.FileInfo(prmLocalFilePath);


                //FtpWebRequest object from Uri
                _ftpRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(prmFtpFilePath));


                //Permission Credential 
                _ftpRequest.Credentials = new System.Net.NetworkCredential(prmUser.Trim(), prmPassword.Trim());

                //Close connection after the command is executed
                _ftpRequest.KeepAlive = prmKeepAliveConnection;

                //Set timeout
                // _ftpRequest.Timeout = 60;

                //Upload command
                _ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

                //transfer type
                _ftpRequest.UseBinary = true;

                //file size
                _ftpRequest.ContentLength = _fileInfo.Length;

                _ftpRequest.UsePassive = prmUsePassive;


                //set buffer size
                const int constBuffLength = 2048;
                int calculatedBuffLength = 0;
                long curLengthPosition = 0;

                byte[] buff = new byte[constBuffLength];


                //open file stream  to read the local file
                using (System.IO.FileStream _fstream = _fileInfo.OpenRead())
                {
                    //dd
                    using (System.IO.Stream _stream = _ftpRequest.GetRequestStream())
                    {

                        int _contentLength = 0;
                        do
                        {
                            //Determining the read and write buffer
                            //==========================================================================================
                            if (_fileInfo.Length < constBuffLength)
                            {
                                //initial file size is less then buffer size
                                calculatedBuffLength = (int)_fileInfo.Length;
                            }
                            else
                            {
                                //Bigger file
                                if ((curLengthPosition + constBuffLength) > _fileInfo.Length)
                                {
                                    //last chunk of the file
                                    calculatedBuffLength = (int)(_fileInfo.Length - curLengthPosition);
                                }
                                else
                                {
                                    calculatedBuffLength = constBuffLength;
                                }

                            }
                            curLengthPosition = curLengthPosition + calculatedBuffLength;
                            //==========================================================================================


                            //Read from filestrem
                            _contentLength = _fstream.Read(buff, 0, calculatedBuffLength);
                            if (_contentLength == 0)
                            {
                                break;
                            }

                            //write content from the file stream to the FTP upload stream
                            _stream.Write(buff, 0, calculatedBuffLength);

                        } while (_contentLength != 0);

                    }

                }

                System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)_ftpRequest.GetResponse();

                _ftpRequest = null;

                if (response != null)
                {
                    return response;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                _ftpRequest = null;
            }
        }
    }
}
